 <script>
        //1.定义LinkList类
        class Node {
            constructor(element) {
                //保存元素
                this.element = element;
                //指向下个节点
                this.next = undefined;

            }
        }

        class LinkedList {
            constructor() {
                this.head = undefined;
                this.length = 0;
            }

            //1.从链表尾部插入元素

            push(element) {
                    const newNode = new Node(element);
                    let current;
                    //分情况讨论
                    //1.插入值为第一个元素
                    if (this.head == null) {
                        this.head = newNode;
                    } else {
                        current = this.head;
                        while (current.next != null) {
                            current = current.next;

                        }
                        current.next = newNode;
                    }
                    this.length++;


                }
                //2.从中间任意位置插入元素
            insert(position, element) {

                if (position < 0 || position > this.length) {
                    return false;
                }
                const node = new Node(element);
                if (position === 0) {
                    const current = this.head
                    node.next = current
                    this.head = node;
                } else {
                    let index = 0;
                    let current = this.head;
                    let previous = null;
                    while (index++ < position) {
                        previous = current;
                        current = current.next;
                    }
                    previous.next = node;
                    node.next = current;

                }
                this.length++;
                return true

            }
            getElementAt(position) {
                if (position >= 0 && position <= this.length) {
                    let index = 0;
                    let current = this.head;
                    while (index++ < position) {
                        current = current.next;
                    }
                    return current;
                }
                return undefined;
            }
            removeAt(position) {
                //1.检查传入参数的有效性 position[0,length-1]才是有效的
                if (position >= 0 && position < this.lenght) {
                    //进行处理
                    //1.position===0
                    let current = this.head;
                    if (position === 0) {
                        this.head = current.next;
                        this.length--;
                        return current.element;

                    } //2.移除其他位置元素
                    else {
                        let previous = this.previous.getElementAt(index - 1);
                        let current = this.getElementAt(index);

                        previous.next = current.next;

                    }
                    this.length--;
                    return current.element;
                }
                return undefined;
            }

            //indexof(element)返回，功能:传入一个元素，返回元素的位置
            indexof(data) {
                    let current = this.head;
                    let index = 0;
                    while (current) {
                        if (current.element === data) {
                            return index;
                        }
                        index++;
                        current = current.next;
                    }
                    return -1;
                }
                //remove(element)
            remove(data) {
                let index = this.indexof(data);
                //分情况讨论
                if (index === -1) {
                    return;
                }
                this.removeAt(index);
            }
            toString() {
                //按照情况去进行处理
                if (this.head == null) {
                    return '';

                }
                let str = this.head.element;
                let current = this.head.next;
                while (current != null) {
                    str = str + ',' + current.element;
                    current = current.next;
                }
                return str;
            }

        }

        const list = new LinkedList();
        list.push('a');
        list.push('b');
        list.push('c'); // console.log(list.removeAt(0));
        console.log(list);
        console.log(list.getElementAt(2));
        list.removeAt(2);
        console.log(list.removeAt(1));
        console.log(list);
        console.log(list.indexof('c'));
        console.log(list.remove('c'));
        console.log(list.toString());
    </script>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // //1.对自身调用
        // function f1(para1, para2) {
        //     f1(para1, para2)
        // }
        // //2.间接对自身调用
        // function f1() {
        //     fn2()
        // }

        // function f2() {
        //     fn1()
        // }
        // //递归一定有基线
        // function test() {
        //     test();
        // }

        //求n的阶乘
        //输入：n
        //输出：n!
        // function factorial(n) {
        //     //1.基线
        //     if (n === 1) {
        //         return n;
        //     }
        //     //2.自身调用

        //     // const result;
        //     result = n * factorial(n - 1);
        //     return result;
        // }
        // console.log(factorial(3));

        // //递归斐波那契
        // //1 1 2 3 5 8 13
        // function fibo(n) {
        //     // if (n === 0 || n === 1) {
        //     //     return n;
        //     // }
        //     if (n === 1 || n === 2) {
        //         return 1;
        //     }

        //     return fibo(n - 1) + fibo(n - 2);
        // }
        // console.log(fibo(5))

        class Node {
            constructor(element) {
                this.element = element;
                this.next = undefined;
            }
        }
        class LinkedList {
            constructor() {
                this.head = undefined;
            }
            append(arr) {
                let node = new Node(arr[0]);
                this.head = node;
                for (let i = 1; i < arr.length; i++) {
                    //数组循环储存数据
                    node.next = new Node(arr[i]);
                    //列表的节点循环
                    node = node.next
                }
            }
            reserve(head) {
                    //1.基线条件
                    if (head.next == null) {
                        return head;
                    }
                    //2.调用自身
                    let restNode = this.reserve(head.next);

                    //逆置功能
                    let current = head.next;
                    current.next = head;
                    head.next = undefined;

                    this.head = restNode;
                    return this.head;
                }
                // reserve(list.head);
            toString() {
                //按照情况去进行处理
                if (this.head == null) {
                    return '';

                }
                let str = this.head.element;
                let current = this.head.next;
                while (current != null) {
                    str = str + ',' + current.element;
                    current = current.next;
                }
                return str;
            }

        }

        const arr = [1, 2, 3, 4];
        const list = new LinkedList();
        list.append(arr);
        list.reserve(list.head);
        console.log(list.toString());
    </script>